﻿using BLL;
using Inventory.BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PSIMIS2019.SubPages
{
    public partial class _Product : PSIMIS2019.Templates.BaseInfoForm
    {
        private ProductBLL bl;         //业务逻辑访问类对象
        private Product product;       //业务实体类对象
        private List<Product> list;    //建立泛型集合对象
        public _Product()
        {
            InitializeComponent();
            bl = new ProductBLL();     //实例化业务逻辑类
            list = new List<Product>();//实例化泛型集合
            BindData();
        }
        /// <summary>
        /// 控件可操作性
        /// </summary>
        /// <param name="opera"></param>
        private void ControlOpera(string opera)
        {
                txtProductName.Enabled   =
                txtSpellingCode.Enabled  =
                txtUnit.Enabled          =
                txtOrigin.Enabled        =
                txtPurchasePrice.Enabled =
                txtQuantity.Enabled      = true;
            if(opera=="U" || opera == "D")
                txtProductID.Enabled = false;
            else
                txtProductID.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ControlOpera("I");
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            ControlOpera("U");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ControlOpera("D");
        }
        /// <summary>
        /// 将UI控件中的值传给Product对象，形成一条/一行记录信息
        /// </summary>
        private Product SetValue()
        {
            product = new Product();
            product.ProductID = txtProductID.Text.Trim();
            product.ProductName = txtProductName.Text.Trim();
            product.SpellingCode = txtSpellingCode.Text.Trim();
            product.BarCode = txtBarCode.Text.Trim();
            product.Special = txtSpecial.Text.Trim();
            product.Unit = txtUnit.Text.Trim();
            product.Origin = txtOrigin.Text.Trim();
            product.PurchasePrice = decimal.Parse(txtPurchasePrice.Text.Trim());
            product.SalePrice = decimal.Parse(txtSalePrice.Text.Trim());
            product.Quantity = int.Parse(txtQuantity.Text.Trim());
            product.Category = new Category();
            product.Category.CategoryID = cbCategory.SelectedValue.ToString();
            return product;
        }

        private void cbCategory_VisibleChanged(object sender, EventArgs e)
        {
            DataSet cList = bl.CategoryList();
            cbCategory.DataSource = cList.Tables[0];
            cbCategory.DisplayMember = "CategoryName";
            cbCategory.ValueMember = "CategoryID";
        }
        private string Validation()
        {
            if (txtProductID.Text.Trim().Length == 0) { txtProductID.Focus(); return "产品编号不允许为空！"; }
            //请大家自行完成其它控件数据验证
            return "";
        }
        private void BindData()
        {
            dgvProduct.DataSource = null;
            list = bl.GetList();
            dgvProduct.DataSource = list;
        }
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            txtProductID.Text   = product.ProductID.Trim();
            txtProductName.Text = product.ProductName.Trim();
            txtSpellingCode.Text = product.SpellingCode.Trim();
            txtBarCode.Text      = product.BarCode.Trim();
            txtSpecial.Text      = product.Special.Trim();
            txtUnit.Text          = product.Unit.Trim();
            txtOrigin.Text          = product.Origin.Trim();
            txtPurchasePrice.Text  = product.PurchasePrice.ToString();
            txtSalePrice.Text    = product.SalePrice.ToString();
            txtQuantity.Text        = product.Quantity.ToString();

            cbCategory.Text = product.Category.CategoryName;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值完成一行记录的对象赋值
                if (bl.Insert(product))
                {
                    BindData();
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (bl.Update(product))
                {
                    BindData();
                    ShowInfo();//将更新数据进行控件绑定
                    MessageBox.Show("更新数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("更新数据失败！", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (bl.Delete(product))
                {
                    BindData();
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("删除数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("删除数据失败！", "系统提示");
            }
        }
        //根据产品名称自动生成拼音码
        private void txtProductName_TextChanged(object sender, EventArgs e)
        {
            txtSpellingCode.Text = PY.HZToSpell(txtProductName.Text.Trim());
        }
        //根据购入价格自动填充销售价格，销售价格是购入价格的30%盈利
        private void txtPurchasePrice_TextChanged(object sender, EventArgs e)
        {
            if(txtPurchasePrice.Text.Length>0)
                txtSalePrice.Text = (double.Parse(txtPurchasePrice.Text.Trim()) * 1.3).ToString();
        }
        //条码生成方法
        //注意：此条码生成并不符合条码生成规范
        private string CreateBarCode()
        {
            //Ticks——当前实例日期和时间的周期数
            //ToFileTimeUtc()——将DateTime值转算成Windows文件时间
            //GetHashCode()——根据此实例的哈希代码生成
            string val = DateTime.Now.GetHashCode().ToString() + System.Environment.TickCount;
            //截取val右边13个字符
            val = val.Substring(val.Length - 13);
            return val;
        }

        private void txtBarCode_VisibleChanged(object sender, EventArgs e)
        {
            txtBarCode.Text = CreateBarCode();
        }
        //1：缺少下拉菜单选择后，产品类型ID值的获取方法实现
        //   事件对应下拉控件SelectedIndexChanged事件
        //2：缺少 DataGridView类别内容显示实现
        //   实现方法参考_Customer.cs的dgvCustomer_CellFormatting事件实现
    }
}
